import 'package:extended_text_library/extended_text_library.dart';
import 'package:flutter/gestures.dart';
import 'package:flutter/material.dart';
import 'package:osc/common/regex_constants.dart';
import 'package:osc/utils/special_text/special_text_bean.dart';
import 'package:osc/utils/string_util.dart';

import '../regex_util.dart';

///<a href='https://www.oschina.net/p/flutter' class='project' target='_blank' title='移动开发平台Flutter'>#Flutter#</a>
///#xx# --> https://www.oschina.net/action/apiv2/detail?ident=fluttergo&key=osc_1_0&type=1
///
///<a href='https://www.oschina.net/tweet-topic/%E6%88%91%E5%A4%AA%E9%9A%BE%E4%BA%86' target='_blank'>#我太难了#</a>
///#xx# -->https://www.oschina.net/action/apiv2/topic_tweets?name=daily%2Btips&topicId=0&type=2 (type 1最新， 2最热)
///
///<a href="https://my.oschina.net/u/1272314" class="referer" target="_blank">@胖达panda</a>
///@xx ---> https://www.oschina.net/action/apiv2/user_info?nickname=%E8%83%96%E8%BE%BEpanda
///
///<a href='https://m.baidu.com/' rel='nofollow' target='_blank'>https://m.baidu.com/</a>
///<a href='http://music.163.com/song/545350941/?userid=1609832540' rel='nofollow' target='_blank'>http://music.163.com/song/545350941/?userid=1609832540</a>
///rel='nofollow' --> url ---> 打开 url

class CommonHrefText extends SpecialText {

  final int start;

  /// whether show background for @somebody|#something#|url
  final bool showAtBackground;

  int type;
  int id;
  String value;
  CommonHrefText({@required String beginFlag, @required String stopFlag, @required TextStyle textStyle, @required SpecialTextGestureTapCallback onTap,
      this.showAtBackground: false, this.start, @required this.type = SpecialTextBean.TYPE_NONE})
      : super(beginFlag, stopFlag, textStyle, onTap: onTap);

  @override
  InlineSpan finishText() {

    String redundant = endFlag.substring(0, endFlag.length-1);//当endFlag length > 1, 会产生多余内容

    String allText = toString();
    allText = allText.replaceFirst(redundant, "");

    String href = RegexUtil.getAttrValueFromHtmlTag(allText, ".*", "href");
//    type = SpecialTextBean.getType(allText);
    String resultText = StringUtil.clearHtmlContent(allText);
    switch(type){
      case SpecialTextBean.TYPE_PROJECT:
        value = href.substring(href.lastIndexOf("/")+1);
        break;
      case SpecialTextBean.TYPE_USER:
        value = resultText.substring(1);
        if(RegexUtil.isMatch(RegexConstants.REGEX_NEGATIVE_INTEGER, href.substring(href.lastIndexOf("/")+1))) {
          id = int.parse(href.substring(href.lastIndexOf("/") + 1));
        }
        break;
      case SpecialTextBean.TYPE_TOPIC:
        value = resultText.substring(1, resultText.length - 1);
        break;
      case SpecialTextBean.TYPE_URL:
        value = href;
        break;
    }

    SpecialTextBean bean = SpecialTextBean(type: type, value: value, id: id);

    return showAtBackground
        ? BackgroundTextSpan(
            background: Paint()..color = Colors.green.withOpacity(0.15),
            text: resultText,
            actualText: resultText,
            start: start,

            ///caret can move into special text
            deleteAll: true,
            style: textStyle,
            recognizer: (TapGestureRecognizer()
              ..onTap = () {
                if (onTap != null) onTap(bean);
              }))
        : SpecialTextSpan(
            text: resultText,
            actualText: resultText,
            start: start,
            style: textStyle,
            recognizer: (TapGestureRecognizer()
              ..onTap = () {
                if (onTap != null) onTap(bean);
              }));
  }
}
